import coin.exchange.base.kr_rest.native_public_client_base as npubcb
import coin.exchange.upbit_v1.kr_rest.constants as exchange_constants


class UpbitNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self):
    npubcb.NativePublicClientBase.__init__(self, url="https://api.upbit.com/v1")
    self._valid_minitue_kline_period = [1, 3, 5, 15, 10, 30, 60, 240]

  def check_minite_kline_period(self, kline_period):
    assert kline_period in self._valid_minitue_kline_period, \
        'Invalid kline period!'

  def query_market_all(self):
    return self._get_as_json("market/all")

  def query_ticker(self, market):
    return self._get_as_json("ticker", params=dict(markets=market))

  def get_history_kline_minite(self, market, kline_period, count, end_time):
    self.check_minite_kline_period(kline_period)
    url = "candles/minutes/%i" % kline_period
    params = {'market': market, 'count': count, 'to': end_time.strftime('%Y-%m-%d %H:%M:%S')}
    return self._get_as_json(url, params=params)

  def get_history_kline_day(self, market, count, end_time):
    url = "candles/days"
    params = {'market': market, 'count': count, 'to': end_time.strftime('%Y-%m-%d %H:%M:%S')}
    return self._get_as_json(url, params=params)

  def queryAll(self):
    print(self.query_market_all())
    for symbol in exchange_constants.native_symbol_list_for_test:
      print(self.query_ticker(symbol))
